package com.intelligence.common.system;


import com.intelligence.entity.pojo.SysPermission;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {


    //使用递归方法创建菜单
    public static List<SysPermission> buildTree(List<SysPermission> menus) {
        List<SysPermission> trees = new ArrayList<>();
        //遍历菜单将父节点是0的菜单添加
        for (SysPermission permission : menus) {
            if (permission.getParentid() == 0l) {
                trees.add(findChildren(permission,menus));
            }
        }
        return trees;
    }
    //查找子菜单
    public static SysPermission findChildren(SysPermission permission,List<SysPermission> permissionList) {
        System.out.println(permission);
        permission.setChildren(new ArrayList<>());
        for (SysPermission permission1: permissionList) {
            if (permission1.getParentid().longValue() == permission.getId().longValue()) {
                permission.getChildren().add(findChildren(permission1,permissionList));
            }
        }
        return permission;
    }
}
